<template>
    <view class="cu-modal" :class="showDialog ? 'show': '' ">
        <view class="cu-dialog text-grey" style="width:85%;">
            <view class="cu-bar bg-white justify-end">
                <view class="content">授权获取用户信息</view>
                <view class="action" @tap="hide">
                    <text class="cuIcon-close"></text>
                </view>
            </view>
            <view class="padding">
                <view class="text-center"><img :src="$tools.j('logo.png')" style="width:60px;height:50px;"/></view>
                <view class="margin-top">
                    <view class="text-grey">获取你的公开信息（昵称、头像等)</view>
                </view>
            </view>
            <view class="cu-bar padding">
                <view class="action margin-0 flex-sub  solid-left" @tap="">
                    <button class="cu-btn lg bg-red radius response" open-type='getUserInfo' @getuserinfo='bindGetUserInfo'>微信授权</button>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
    export default {
        props:{
            wxloginInfo       :{
                type:Object
            }
        },
        data(){
           return  {
               showDialog:false,
               redirectUrl:''
            }
        },
        methods:{
            show(){
                this.showDialog = true
            },
            hide(){
                this.showDialog = false
            },
            setRedirectUrl(url){
                this.redirectUrl = url
                return this
            },
            bindGetUserInfo(e){
                console.log(e)
                if (e.detail.userInfo){
                    let { encryptedData, iv } = e.detail
                    this.$user.wx_login_info().then(res=>{
                        if(res){
                            console.log('111111')
                            console.log('session_key')
                            console.log(res)
                            let  { session_key ,cache_key } = res
                            this.$api.wxxcxUserInfo({ encryptedData, iv ,session_key,cache_key}).then(res1=>{
                                if(res1.status===200){
                                    this.$store.dispatch('user/authorize',res1).then(res2=>{
                                        if(res2){
                                            console.log('授权登录成功')
                                            this.$emit('loginOk',this.redirectUrl)
                                            this.hide();
                                        }
                                    })
                                }
                            })
                        }else{
                            console.log('2222')
                        }
                    })
                }

            }

        },
        created() {
        }
    }
</script>

<style scoped>

</style>
